package com.xjj.servicejob.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjj.entity.Job;
import com.xjj.servicejob.vo.JobVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author sjy
 * @create 2022-10-20 11:30
 * 作业的mapper
 */
public interface JobMapper extends BaseMapper<Job> {
    /**
     * 修改已经发布的作业
     *
     * @param job
     */
    @UpdateProvider(type = JobMapper.JobSqlProvider.class, method = "update")
    void update(Job job);

    /**
     * 多条件分页查询
     *
     * @param job
     * @return
     */
    @SelectProvider(type = JobMapper.JobSqlProvider.class, method = "getByCondition")
    List<Job> getByCondition(Job job);

    /**
     * 提交一次修改一次作业提交人数
     *
     * @param id
     */
    @Update("update xjj_job set submitted_num = submitted_num+1 where id = #{id}")
    void updateSubmittedNum(Integer id);

    /**
     * 根据班级id和时间查询今日作业
     * @param klassId
     * @param currentDay
     * @return
     */
    @Select("select * from xjj_job where start_time = #{currentDay} and klass_id = #{klassId}")
    List<Job> getTodayAllJob(@Param("klassId") Integer klassId, @Param("currentDay") String currentDay);

    /**
     * sql 语句构建器；
     * 根据传入的 goods（封装了条件）、method，产生并返回对应的动态 sql 语句
     */
    @Slf4j
    public static class JobSqlProvider {
        public String update(Job job) {
            String sql = new SQL() {
                {
                    UPDATE("xjj_job");
                    if (job.getKlassId() != null) {
                        SET("klass_id = #{klassId}");
                    }
                    if (job.getStatus() != null) {
                        SET("status = #{status}");
                    }
                    if (job.getStartTime() != null) {
                        SET("start_time=#{startTime}");
                    }
                    if (job.getEndTime() != null) {
                        SET("end_time=#{endTime}");
                    }
                    if (job.getContent() != null) {
                        SET("content=#{content}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
            System.out.println(sql);
            return sql;
        }

        public static String getByCondition(Job job) {
            String sql = new SQL() {
                {
                    SELECT("id", "start_time", "manager_id","end_time", "send_time", "status", "content", "submitted_num", "klass_id" );
                    FROM("xjj_job");
                    if (job != null) {
                        if (job.getStatus() != null) {
                            WHERE("status = #{status}");
                        }
                        if (job.getKlassId() != null) {
                            WHERE("klass_id = #{klassId}");
                        }
                        if (job.getFindStartTime() != null) {
                            WHERE("start_time >= #{findStartTime}");
                        }
                        if (job.getFindEndTime() != null) {
                            WHERE("end_time >= #{findEndTime}");
                        }
                        if (job.getFindSendStartTime() != null) {
                            WHERE("send_time >= #{findSendStartTime}");
                        }
                        if (job.getFindSendEndTime() != null) {
                            WHERE("send_time >= #{findSendEndTime}");
                        }
                        if (job.getManagerId() != null) {
                            WHERE("manager_id=#{managerId}");
                        }
                    }
                }
            }.toString();
            System.out.println(sql);
            return sql;
        }
    }
}